require(['init'], function () {
    var $pagination = $('#js-pagination');
    var pagination;
    var jsondata;

    function getpotential_guest_list(querystr, cb) {
        var url = "/api/marketing/leads";
        var query = {}
        if(querystr) {
            query = querystr
        }
        ajaxMethod({
            url: url,
            data: query,
            success: function (data) {
                console.log(data)
                ajaxMethod({
                    url: "/api/orders",
                    type: "get",
                    success: function (json) {
                        cb && cb(data)
                        if(data.list.length == 0) {
                            $('#J_guestlist').html('<div style="margin-top: 50px;text-align: center;">'+i18next.t('input.nullresults')+'</div>');
                            $pagination.hide()
                            return;
                        }
                        var $guestListTable = $('#J_guestlist').html('');
                        var $thead = $('<thead>\
                          <tr>\
                            <th>'+i18next.t('table.no')+'</th>\
                            <th>'+i18next.t('table.registration')+'</th>\
                            <th>'+i18next.t('table.markettile')+'</th>\
                            <th>'+i18next.t('table.name')+'</th>\
                            <th>'+i18next.t('table.phone')+'</th>\
                            <th>'+i18next.t('table.origin')+'</th>\
                            <th>'+i18next.t('table.service')+'</th>\
                            <th>'+i18next.t('table.record')+'</th>\
                            <th>'+i18next.t('info.classinaccountnumber')+'</th>\
                            <th>'+i18next.t('table.test')+'</th>\
                            <th>'+i18next.t('table.purchaserecord')+'</th>\
                           <th>'+i18next.t('table.coursearrangement')+'</th>\
                           <th>'+i18next.t('table.handle')+'</th>\
                          </tr>\
                        </thead>').appendTo($guestListTable)
                        var $tbody = $('<tbody></tbody>').appendTo($guestListTable)
                        $.each(data.list, function (index, item) {
                            var id = item.id;
                            var a = JSON.stringify(item.created_at)
                            var day = (item.created_at).replace(/\-/g, "");
                            var $tr = $("<tr data-id=" + day + " ></tr>").appendTo($tbody);
                            $tr.append('<td>1</td>');
                            $tr.append('<td>' + item.created_at + '</td>');
                            $tr.append('<td>' + item.posttitle + '</td>');
                            $tr.append('<td>' + item.name + '</td>');
                            $tr.append('<td>' + item.number + '</td>');
                            var $source = $('<td></td>').appendTo($tr);
                            $tr.append('<td>' + item.staff + '</td>');
                            var $common_record = $('<td><a href="common_recordlist.html?id=' + id + '&name=' + item.name + '" style="color:#398dee">'+i18next.t('table.details')+'</a></td>').appendTo($tr);
                            $tr.append('<td>' + item.ClassInNo + '</td>');
                            $devices = $('<td class="devices_td"></td>').appendTo($tr); //设备检测
                            var $buy = $('<td></td>').appendTo($tr); //购买记录
                            var $curriculum = $('<td></td>').appendTo($tr); //排课情况
                            var ordermobile, commomnumber;
                            commomnumber = item.number;
                            var $div = $('<div class="postionorderlisr warporder"></div>').appendTo($buy);
                            // $div.hover(function(){
                            //   ajaxMethod({
                            //     url:'/api/orders',
                            //     type:'get',
                            //     data:{
                            //       mobile:item.number
                            //     },
                            //     success:function(data){
                            //       var $table=$('<div class="orderlistnumber"></div>').appendTo($div);
                            //       var $tab=$('<table class="table enrollment table-hover">\
                            //         <thead>\
                            //           <tr>\
                            //             <th>No</th>\
                            //             <th>价格</th>\
                            //             <th>产品名称</th>\
                            //           </tr>\
                            //         </thead>\
                            //         </table>').appendTo( $table)
                            //         var $tb=$('<tbody class="tbody"></tbody>').appendTo($tab);
                            //       $.each(data.list,function(i,t){
                            //         var $tr=$('<tr></tr>').appendTo($tb);
                            //         $tr.append('<td>'+(i+1)+'</td>')
                            //         $tr.append('<td>'+(t.amount||'')+'</td>')
                            //         $tr.append('<td>'+(t.productname||'')+'</td>')
                            //       })
                            //     }
                            //   })
                            // },function(){
                            //       $('.orderlistnumber').hide()
                            //   })
                            var flag = false;
                            $.each(json, function (int, ite) {
                                ordermobile = ite.mobile;
                                if (ordermobile == commomnumber) {
                                    var $hasy = $('<a style="color:#398dee" class="postionorderlisr warporder">'+i18next.t('table.haverecords')+'</a>').appendTo($div);
                                    flag = true;
                                    return false;
                                    $hasy.hover(function () {
                                        ajaxMethod({
                                            url: '/api/orders',
                                            type: 'get',
                                            data: {
                                                mobile: item.number
                                            },
                                            success: function (data) {
                                                var $table = $('<div class="orderlistnumber"></div>').appendTo($div);
                                                var $tab = $('<table class="table enrollment table-hover">\
                                                <thead>\
                                                  <tr>\
                                                    <th>No</th>\
                                                    <th>价格</th>\
                                                    <th>产品名称</th>\
                                                  </tr>\
                                                </thead>\
                                                </table>').appendTo($table)
                                                var $tb = $('<tbody class="tbody"></tbody>').appendTo($tab);
                                                $.each(data.list, function (i, t) {
                                                    var $tr = $('<tr></tr>').appendTo($tb);
                                                    $tr.append('<td>' + (i + 1) + '</td>')
                                                    $tr.append('<td>' + (t.amount || '') + '</td>')
                                                    $tr.append('<td>' + (t.productname || '') + '</td>')
                                                })

                                            }
                                        })
                                    }, function () {
                                        $('.orderlistnumber').hide()
                                    })
                                }
                            })
                            if (!flag) {
                                $('<a>'+i18next.t('table.norecord')+'</a>').appendTo($buy)
                            }
                            var $td = $('<td></td>').appendTo($tr);
                            var $addenroll=$('<a class="btn btn-primary btn-sm btn-inverse" style="margin-right:15px;" disabled="disabled">'+i18next.t('table.auditions')+'</a>').appendTo($td);//安排试听课
                            if(item.ClassInNo!=null){
                              $addenroll.removeAttr("disabled");
                              $addenroll.on('click',function(){
                                var $dom=$('<div class="formList">\
                                 <div class="formLi">\
                                      <div class="formTitle required">\
                                          <span>'+i18next.t('table.name')+'</span>\
                                      </div>\
                                      <div class="formContent">\
                                          <input class="form-control" data-formcheck="length" type="text" name="enroll_name"  value='+item.name+' readonly="readonly" style="width: 300px;">\
                                      </div>\
                                  </div>\
                                  <div class="formLi">\
                                       <div class="formTitle required">\
                                           <span>'+i18next.t('info.classinaccountnumber')+'</span>\
                                       </div>\
                                       <div class="formContent">\
                                           <input class="form-control" data-formcheck="length" type="text" name="ClassIn"  value='+(item.ClassInNo==""?"请填写ClassIn账号":item.ClassInNo)+' readonly="readonly" style="width: 300px;">\
                                       </div>\
                                   </div> \
                                 <div class="formLi">\
                                        <div class="formTitle required">\
                                            <span>'+i18next.t('table.phone')+'</span>\
                                        </div>\
                                        <div class="formContent">\
                                            <input class="form-control" data-formcheck="length" type="text" name="enroll_number"  value='+item.number+' readonly="readonly" style="width: 300px;">\
                                        </div>\
                                    </div></div>');
                                var popUpBox=new PopUpBox({
                                  type:'dom',
                                  style:'default contentScroll',
                                  submitBtnType:'alert',
                                  titleText:i18next.t('popupbox.aboutclass'),
                                  drag:true,
                                  show:true,
                                  content: $dom,
                                  callback:{
                                    yes: function (e) {
                                      ajaxMethod({
                                        url: '/api/demoment',
                                        method: 'post',
                                        data:{
                                         mobile:item.number,
                                         chinesename:item.name,
                                         ClassInNo:item.ClassInNo
                                        },
                                        success: function (json) {
                                         formid=json.formuid;
                                         //location.reload()
                                         ajaxMethod({
                                             url: '/api/marketing/lead/'+item.id,
                                             type: 'put',
                                             data: {
                                               id:item.id,
                                               schedule:formid,
                                               number:item.number,
                                               name:item.name,
                                               ClassInNo:item.ClassInNo
                                             },
                                             success: function (data,status) {
                                               location.reload()
                                               //location.replace("potential_guest_list.html");
                                               if(status.code!=1){

                                               }
                                             }
                                         });
                                        },
                                        error:function(){
                                          alert('no')
                                        }
                                      })

                                    }
                                  },
                                  position:{
                                    width:530,
                                    height:265
                                  }
                                })
                               $(".submitBtnBox").find('.btn').css('margin-left','240px')
                              })
                            }
                            if (item.schedule == 0) {
                                $('<a>'+i18next.t('table.notimetable')+'</a>').appendTo($curriculum);
                            } else {
                                $('<a style="color:#398dee;">'+i18next.t('table.scheduledcourses')+'</a>').appendTo($curriculum);
                                // $('<a style="margin-right:15px;" class="btn btn-primary btn-sm btn-inverse" disabled="disabled">安排试听课</a>').appendTo($td)
                            }
                            if (item.source == 0) {
                                $('<a>'+i18next.t('table.wechat')+'</a>').appendTo($source)
                            } else if (item.source == 1) {
                                $('<a>'+i18next.t('table.offline')+'</a>').appendTo($source)
                            } else if (item.source == 2) {
                                $('<a>'+i18next.t('table.official')+'</a>').appendTo($source)
                            }
                            var sourcetext = $source.text();
                            console.log(sourcetext)
                            var $edit = $('<a href="potential_guest.html?id=' + id + '&sourcetext=' + sourcetext + '" class="btn btn-primary btn-sm btn-inverse">'+i18next.t('table.compile')+'</a>').appendTo($td);
                            //设备加测
                            var device = item.device;
                            console.log(item.device)
                            if (device == null) {
                                return;
                            }
                            $.each(device, function (iden, ite) {
                                console.log(ite)
                                if (ite == null) {
                                    return;
                                }
                                //用户选择的摄像头状态(1正常，2不正常)
                                var $show = "";
                                var $headsetshow = "";
                                var $microphoneshow = "";
                                if (ite.CameraArbitrary == 1) {
                                    var $CameraArbitrary = $('<a style="position:relative;top:0;left:0"><img src="../admin/img/camera.png"/></a>').appendTo($devices);
                                    $CameraArbitrary.hover(function () {
                                        $show = $('<div style="position:absolute;z-index:1000;" class="CameraArbitrary">\
                                        <ul>\
                                           <li>' + item.created_at + '</li>\
                                          <li>\
                                            <label>'+i18next.t('table.serselection')+'：</label>\
                                            <a>'+i18next.t('table.normal')+'</a>\
                                          </li>\
                                          <li>\
                                            <label>'+i18next.t('table.programdetection')+'：</label>\
                                            <a>'+i18next.t('table.normal')+'</a>\
                                          </li>\
                                          <li>\
                                            <label>'+i18next.t('table.uploadreason')+'：</label>\
                                            <a>'+i18next.t('table.checkcompleted')+'</a>\
                                          </li>\
                                          <li>\
                                            <label>'+i18next.t('table.sedevice')+'：</label>\
                                            <a>' + ite.EEO_DEVICE_LIST.camera + '</a>\
                                          </li>\
                                          <li>\
                                            <label>'+i18next.t('table.devicelist')+'：</label>\
                                            <a>' + ite.EEO_DEVICE_LIST.camera + '</a>\
                                          </li>\
                                        </ul>\
                                        </div>').appendTo($CameraArbitrary)
                                    }, function () {
                                        $show.hide();
                                    })
                                } else if (ite.CameraArbitrary == 2) {
                                    var $CameraArbitrary = $('<a style="position:relative;top:0;left:0"><img src="../admin/img/camerabead.png"/></a>').appendTo($devices);
                                    $CameraArbitrary.hover(function () {
                                        $show = $('<div style="position:absolute;z-index:1000;" class="CameraArbitrary">\
                                      <ul>\
                                         <li>' + item.created_at + '</li>\
                                        <li>\
                                          <label>'+i18next.t('table.serselection')+'：</label>\
                                          <a>'+i18next.t('table.abnormal')+'</a>\
                                        </li>\
                                        <li>\
                                          <label>'+i18next.t('table.programdetection')+'：</label>\
                                          <a>'+i18next.t('table.abnormal')+'</a>\
                                        </li>\
                                        <li>\
                                          <label>'+i18next.t('table.uploadreason')+'：</label>\
                                          <a>'+i18next.t('table.checkcompleted')+'</a>\
                                        </li>\
                                        <li>\
                                          <label>'+i18next.t('table.sedevice')+'：</label>\
                                          <a>' + ite.EEO_DEVICE_LIST.camera + '</a>\
                                        </li>\
                                        <li>\
                                          <label>'+i18next.t('table.devicelist')+'：</label>\
                                          <a>' + ite.EEO_DEVICE_LIST.camera + '</a>\
                                        </li>\
                                      </ul>\
                                      </div>').appendTo($CameraArbitrary)
                                    }, function () {
                                        $show.hide();
                                    })
                                }

                                //用户选择的耳机状态(1正常，2不正常)
                                if (ite.HeadphoneArbitrary == 1) {
                                    var $headset = $('<a style="position:relative;top:0;left:0"><img src="../admin/img/headset.png"/></a>').appendTo($devices);
                                    $headset.hover(function () {
                                        $headsetshow = $('<div style="position:absolute;z-index:1000;" class="CameraArbitrary">\
                                      <ul>\
                                         <li>' + item.created_at + '</li>\
                                        <li>\
                                          <label>'+i18next.t('table.serselection')+'：</label>\
                                          <a>'+i18next.t('table.normal')+'</a>\
                                        </li>\
                                        <li>\
                                          <label>'+i18next.t('table.programdetection')+'：</label>\
                                          <a>'+i18next.t('table.normal')+'</a>\
                                        </li>\
                                        <li>\
                                          <label>'+i18next.t('table.uploadreason')+'：</label>\
                                          <a>'+i18next.t('table.checkcompleted')+'</a>\
                                        </li>\
                                        <li>\
                                          <label>'+i18next.t('table.sedevice')+'：</label>\
                                          <a>' + ite.EEO_DEVICE_LIST.speaker + '</a>\
                                        </li>\
                                        <li>\
                                          <label>'+i18next.t('table.devicelist')+'：</label>\
                                          <a>' + ite.EEO_DEVICE_LIST.speaker + '</a>\
                                        </li>\
                                      </ul>\
                                      </div>').appendTo($CameraArbitrary)
                                    }, function () {
                                        $headsetshow.hide();
                                    })
                                } else if (ite.HeadphoneArbitrary == 2) {
                                    var $headset = $('<a style="position:relative;top:0;left:0"><img src="../admin/img/headsetbead.png"/></a>').appendTo($devices);
                                    $headset.hover(function () {
                                        $headsetshow = $('<div style="position:absolute;z-index:1000;" class="CameraArbitrary">\
                                      <ul>\
                                         <li>' + item.created_at + '</li>\
                                        <li>\
                                          <label>'+i18next.t('table.serselection')+'：</label>\
                                          <a>'+i18next.t('table.abnormal')+'</a>\
                                        </li>\
                                        <li>\
                                          <label>'+i18next.t('table.programdetection')+'：</label>\
                                          <a>'+i18next.t('table.abnormal')+'</a>\
                                        </li>\
                                        <li>\
                                          <label>'+i18next.t('table.uploadreason')+'：</label>\
                                          <a>'+i18next.t('table.checkcompleted')+'</a>\
                                        </li>\
                                        <li>\
                                          <label>'+i18next.t('table.sedevice')+'：</label>\
                                          <a>' + ite.EEO_DEVICE_LIST.speaker + '</a>\
                                        </li>\
                                        <li>\
                                          <label>'+i18next.t('table.devicelist')+'：</label>\
                                          <a>' + ite.EEO_DEVICE_LIST.speaker + '</a>\
                                        </li>\
                                      </ul>\
                                      </div>').appendTo($CameraArbitrary)
                                    }, function () {
                                        $headsetshow.hide();
                                    })
                                }
                                //用户选择的麦克风状态(1正常，2不正常)
                                if (ite.MicrophoneArbitrary == 1) {
                                    var $microphone = $('<a style="position:relative;top:0;left:0"><img src="../admin/img/microphone.png"/></a>').appendTo($devices);
                                    $microphone.hover(function () {
                                        $microphoneshow = $('<div style="position:absolute;z-index:1000;" class="CameraArbitrary">\
                                      <ul>\
                                         <li>' + item.created_at + '</li>\
                                        <li>\
                                          <label>'+i18next.t('table.serselection')+'：</label>\
                                          <a>'+i18next.t('table.normal')+'</a>\
                                        </li>\
                                        <li>\
                                          <label>'+i18next.t('table.programdetection')+'：</label>\
                                          <a>'+i18next.t('table.normal')+'</a>\
                                        </li>\
                                        <li>\
                                          <label>'+i18next.t('table.uploadreason')+'：</label>\
                                          <a>'+i18next.t('table.checkcompleted')+'</a>\
                                        </li>\
                                        <li>\
                                          <label>'+i18next.t('table.sedevice')+'：</label>\
                                          <a>' + ite.EEO_DEVICE_LIST.micphone + '</a>\
                                        </li>\
                                        <li>\
                                          <label>'+i18next.t('table.devicelist')+'：</label>\
                                          <a>' + ite.EEO_DEVICE_LIST.micphone + '</a>\
                                        </li>\
                                      </ul>\
                                      </div>').appendTo($CameraArbitrary)
                                    }, function () {
                                        $microphoneshow.hide();
                                    })
                                } else if (ite.MicrophoneArbitrary == 2) {
                                    var $microphone = $('<a style="position:relative;top:0;left:0"><img src="../admin/img/microphonebead.png"/></a>').appendTo($devices);
                                    $microphone.hover(function () {
                                        $microphoneshow = $('<div style="position:absolute;z-index:1000;" class="CameraArbitrary">\
                                        <ul>\
                                           <li>' + item.created_at + '</li>\
                                          <li>\
                                            <label>'+i18next.t('table.serselection')+'：</label>\
                                            <a>'+i18next.t('table.abnormal')+'</a>\
                                          </li>\
                                          <li>\
                                            <label>'+i18next.t('table.programdetection')+'：</label>\
                                            <a>'+i18next.t('table.abnormal')+'</a>\
                                          </li>\
                                          <li>\
                                            <label>'+i18next.t('table.uploadreason')+'：</label>\
                                            <a>'+i18next.t('table.checkcompleted')+'</a>\
                                          </li>\
                                          <li>\
                                            <label>'+i18next.t('table.sedevice')+'：</label>\
                                            <a>' + ite.EEO_DEVICE_LIST.micphone + '</a>\
                                          </li>\
                                          <li>\
                                            <label>'+i18next.t('table.devicelist')+'：</label>\
                                            <a>' + ite.EEO_DEVICE_LIST.micphone + '</a>\
                                          </li>\
                                        </ul>\
                                        </div>').appendTo($CameraArbitrary)
                                    }, function () {
                                        $microphoneshow.hide();
                                    })
                                }
                                if (ite.NetworkDelay != null) {
                                    var $network=$('<a><img src="../admin/img/network.png"/></a>').appendTo($devices);
                                    $network.hover(function () {
                                    $networkshow = $('<div style="position:absolute;z-index:1000;" class="CameraArbitrary">\
                                      <ul>\
                                         <li>' + item.created_at + '</li>\
                                        <li>\
                                          <label>'+i18next.t('table.operatingsystem')+'：</label>\
                                          <a>' + ite.OperatingSystem + '</a>\
                                        </li>\
                                        <li>\
                                          <label>'+i18next.t('table.servername')+'</label>\
                                          <a>' + ite.ServerName + '</a>\
                                        </li>\
                                        <li>\
                                          <label>CPU</label>\
                                          <a>' + ite.CPU + '</a>\
                                        </li>\
                                        <li>\
                                          <label>'+i18next.t('table.clientip')+'</label>\
                                          <a>' + ite.ClientIP + '</a>\
                                        </li>\
                                        <li>\
                                          <label>'+i18next.t('table.clientip')+'</label>\
                                          <a>' + ite.ClientIP + '</a>\
                                        </li>\
                                        <li>\
                                          <label>'+i18next.t('table.macaddress')+'</label>\
                                          <a>' + ite.Mac + '</a>\
                                        </li>\
                                        <li>\
                                          <label>'+i18next.t('table.networkdelay')+'</label>\
                                          <a>' + ite.NetworkDelay + '</a>\
                                        </li>\
                                        <li>\
                                          <label>'+i18next.t('table.packetloss')+'</label>\
                                          <a>' + ite.FrameLoss + '</a>\
                                        </li>\
                                      </ul>\
                                      </div>').appendTo($CameraArbitrary)
                                    }, function () {
                                        $networkshow.hide();
                                    })
                                }
                            })
                            //$tbody.append($tr)
                        })
                        $pagination.show()
                        $tbody.find("tr").each(function () {
                            //console.log(this)
                            var tdhtml = $(this).children();
                            $.each(tdhtml, function (i, t) {
                                $(t).text() == 'null' ? $(t).text('--') : $(t).text()
                            })
                        });
                        var len = $('table tr').length;
                        for (var i = 1; i < len; i++) {
                            $('table tr:eq(' + i + ') td:first').text(i);
                        }
                    }
                })
            }

        })
    }

    //搜索
    function loadSearchData(querystr) {
        if(querystr) {
            pagination.filter.postid = querystr;
            pagination.filter.number = querystr;
            pagination.filter.name = querystr;

            pagination.searchData()
        } else {
            pagination.reloadData()
        }
    }

    $('#J_guest_search').on('click', function() {
        var $input = $(this).prev();
        var querystr = $.trim($input.val());
        loadSearchData(querystr)
    })

    $('.searchInput').on('keyup', function(e) {
        if(e.keyCode == 13) {
            var querystr = $.trim($(this).val());
            loadSearchData(querystr)
        }
    })
    pagination = new Pagination($pagination, getpotential_guest_list)
})
